#include "ray.h"
#include "material.h"
#include <math.h>


#include "rtcore/rtcore.h"
#include "intersection.h"
namespace gemren
{
inline float sqr(float a)
{
	return a*a;
}


	embree::Col3f ray::shoot(
		const coord& orig, 
		const coord& dir, 
		const bool fast,
		const float aperture,
		const float focus,
		float rrmin,
		float rrmax)
	{
		intersection i;
		coord ndir = embree::normalize(dir);
		int depth = 0;
		if(!fast)
		{   //do fov effect
			coord var = ran->cosined_hemisphere(-ndir)*aperture;
			ndir = ndir*focus + orig; // focal point
			
			i.abs_pos = orig + var;
			i.outcoming = embree::normalize(ndir - i.abs_pos);
			i.lighting_mode = myscene->get_lighting_mode();
		}
		else
		{
			i.abs_pos = orig;
			i.outcoming = ndir;
		}
		while(myscene->first_intersection(i,i.abs_pos, i.outcoming, i.depth))
		{
			if(fast)
				i.triptr->insidemat->interact_fast(ran, myscene, &i);
			else	
				i.triptr->insidemat->interact(ran, myscene, &i);

			//play russian roulette
			float rr = embree::clamp(i.reflectance, rrmin, rrmax);
			if(i.reflectance == 0 || ran->getf()>rr) break;
			else i.dim_light(1.0f/rr);
		}
		return i.get_light(myscene->get_converter());
	}

}